﻿
using NurseStation.common;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace NurseStation
{
    /// <summary>
    ///     Notifiaction.xaml 的交互逻辑
    /// </summary>
    public partial class Notifiaction : Window
    {
        /// <summary>
        /// 延迟加载对象
        /// </summary>
        private static readonly Lazy<Notifiaction> _lazy =
            new Lazy<Notifiaction>(() => new Notifiaction(), LazyThreadSafetyMode.PublicationOnly);

        /// <summary>
        /// 单例对象
        /// </summary>
        public static Notifiaction Instance
        {
            get { return _lazy.Value; }
        }

        private const byte MAX_NOTIFICATIONS = 8;
        private const double topOffset = 5;//y轴填充距离
        private const double leftOffset = 0;//x轴填充距离
        private readonly ObservableCollection<NotifiactionModel> buffer = new ObservableCollection<NotifiactionModel>();

        private readonly ObservableCollection<NotifiactionModel> NotifiactionList =
            new ObservableCollection<NotifiactionModel>();

        private int count;

        private readonly object notifiactionListLock = new object();//提示消息队列锁

        private Notifiaction()
        {
            InitializeComponent();
            BindingOperations.EnableCollectionSynchronization(NotifiactionList, notifiactionListLock);
            NotificationsControl.DataContext = NotifiactionList;
        }
        /// <summary>
        /// 添加通知消息
        /// 增加消息队列线程锁  2023-03-28
        /// </summary>
        /// <param name="notification"></param>
        public void AddNotifiaction(NotifiactionModel notification)
        {
            try
            {
                notification.Id = count++;
                if (NotifiactionList.Count + 1 > MAX_NOTIFICATIONS)
                    buffer.Add(notification);
                else
                {
                    lock (notifiactionListLock)
                    {
                        NotifiactionList.Add(notification);
                    }
                }

                //Show window if there're notifications
                if (NotifiactionList.Count > 0 && !IsActive)
                {
                    Top = SystemParameters.WorkArea.Top + SystemParameters.WorkArea.Height - NotifiactionList.Count * 90 - topOffset;
                    Left = SystemParameters.WorkArea.Left + SystemParameters.WorkArea.Width - Width - leftOffset;
                    Show();
                }
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error(ex);
            }
        }
        /// <summary>
        /// 移除通知消息
        /// 增加消息队列线程锁  2023-03-28
        /// </summary>
        /// <param name="notification"></param>
        public void RemoveNotification(NotifiactionModel notification)
        {
            try
            {
                if (NotifiactionList.Contains(notification))
                {
                    lock (notifiactionListLock)
                    {
                        NotifiactionList.Remove(notification);
                    }
                }

                if (buffer.Count > 0)
                {
                    lock (notifiactionListLock)
                    {
                        NotifiactionList.Add(buffer[0]);
                    }
                    buffer.RemoveAt(0);
                }

                //Close window if there's nothing to show
                if (NotifiactionList.Count < 1)
                    Hide();
            }
            catch (Exception ex)
            {
                LogHelper.GetLogHelper().Error(ex);
            }
        }

        private void NoticeGrid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Height != 0.0)
                return;
            var element = sender as Grid;
            RemoveNotification(NotifiactionList.First(n => n.Id == int.Parse(element.Tag.ToString())));
        }
    }

    public class NotifiactionModel
    {
        /// <summary>
        ///     Id不需要赋值
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        ///     通知标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        ///     通知内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        ///     通知类型
        /// </summary>
        public EnumPromptType NotifiactionType { get; set; }
    }

    /// <summary>
    ///     提示类型
    /// </summary>
    public enum EnumPromptType
    {
        /// <summary>
        ///     消息
        /// </summary>
        Info,

        /// <summary>
        ///     警告
        /// </summary>
        Warn,

        /// <summary>
        ///     失败
        /// </summary>
        Error,

        /// <summary>
        ///     成功
        /// </summary>
        Success
    }
}